<?php

namespace app\admin\model;
use app\service\Score;
use think\facade\Db;
use think\facade\Session;
use think\Model;

class ArtAdopt extends Model
{
    // 采纳答案
    static public function adopt($art_id , $uid)
    {
        $adopt = self::where('article_id', $art_id);
        // 该文章不存在
        if ($adopt->findOrEmpty()->isEmpty()) return '该文章不存在';
        // 是否已采纳答案了
        $isAdopt = $adopt->find();
         // 判断是否为发布者操作
        if($isAdopt['author_id'] != Session::get('user.user_id')) return '请求错误';
        if ($isAdopt['status'] == 1) return '该文章已完结，无需重复采纳';
        else {
            $art = Article::find($art_id);
            $score = $art['score'];
            self::where('article_id',$art_id)->update(['status' => 1,'user_id' => $uid]);
            // $res = User::where('id',$uid)->inc('score' , $score)->update();
            $title = $art->title;
            $msg = "文章【 $title 】发布者已采纳你的答案，积分已发放到你的账户了哦！";
            Msg::send(Session::get('user.user_id') , $uid , ['content' => $msg]);
            $res = Score::add($uid , $score);
            if($res) return 1;
            else return 0;
        }
    }
}
